<template>
  <!--  <transition name='fade' :show="showFlag">-->
  <view class="popup-wrapper" :class="visibleFlag?'':'hide'" v-if="initFlag">
    <view class="popup" ref="popup">
      <view class="popup-content" :class="[position,animateFlag?('show-'+position):('hide-'+position)]"
            @touchmove.stop="handleMove">
        <view class="popup-title gray" v-if="title">{{ title }}</view>
        <view class="popup-top">
          <slot name="top"></slot>
        </view>
        <scroll-view class="scroll-view" :scroll-top="scrollTop" scroll-with-animation :scroll-y="true" v-if="scrollY">
          <view class="scroll-content">
            <slot/>
          </view>
        </scroll-view>
        <view class="content" v-else>
          <slot/>
        </view>
        <view class="opt">
          <slot name="opt">
            <view class="btn-wrapper" v-if="!simple&&position==='middle'">
              <button class="cancel gray" @click.stop="handleClose" v-if="!onlySubmitBtn">{{ cancelText }}</button>
              <button type="primary" class="submit" :open-type="openType" :loading="loading" @click.stop="handleSubmit">
                {{ submitText }}
              </button>
            </view>
          </slot>
        </view>
        <text v-if="simple" class="iconfont popup-close" @click.stop="handleClose">&#xe67b;</text>
      </view>
    </view>
    <view class="modal" :class="animateFlag?'show-modal':'hide-modal'" @touchmove.stop="handleMove"
          @click.stop="emitClose"></view>
  </view>
  <!--  </transition>-->
</template>

<script>
export default {
  name: 'f-popup',
  props: {
    showFlag: {
      type: Boolean,
      default: false
    },
    loading: {
      type: Boolean,
      default: false
    },
    simple: {
      type: Boolean,
      default: false
    },
    position: {
      type: String,
      default: 'middle'
    },
    title: {
      type: String,
      default: ''
    },
    zIndex: {
      type: [Number, String],
      default: 99
    },
    closeOnClickModal: {
      type: Boolean,
      default: true
    },
    scrollY: {
      type: Boolean,
      default: true
    },
    scrollTopY: {
      type: Number,
      default: -1
    },
    openType: {
      type: String,
      default: ''
    },
    submitText: {
      type: String,
      default: '确定'
    },
    cancelText: {
      type: String,
      default: '取消'
    },
    onlySubmitBtn: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    showFlag() {
      this.setFlag();
    },
    scrollTopY() {
      if (this.scrollTopY !== -1) {
        this.scrollTop = this.scrollTopY;
      }
    }
  },
  computed: {
    transitionName() {

    }
  },
  data() {
    return {
      initFlag: false,
      animateFlag: false,
      visibleFlag: false,
      scrollTop: 0
    };
  },
  methods: {
    setFlag() {
      if (this.showFlag) {
        this.initFlag = true;
        this.visibleFlag = true;
        clearTimeout(this.timer);
        setTimeout(() => {
          this.animateFlag = true;
        }, 20);
      } else {
        this.animateFlag = false;
        this.timer = setTimeout(() => {
          this.visibleFlag = false;
        }, 200);
      }
    },
    handleMove() {
      return false;
    },
    emitClose(e) {
      if (this.closeOnClickModal) {
        this.$emit('update:showFlag', false);
        this.$emit('cancel');
      }
    },
    handleSubmit() {
      this.$emit('submit');
    },
    handleClose() {
      this.$emit('update:showFlag', false);
      this.$emit('cancel');
    },
    setScrollTop(scrollTop = 0) {
      setTimeout(() => {
        if (this.scrollTop === scrollTop) {
          this.scrollTop++;
        } else {
          this.scrollTop = scrollTop;
        }
      });
    }
  },
  mounted() {
    this.setFlag();
  },
  onLoad() {
  },
  onUnload() {
  }
};
</script>

<style lang="scss" scoped>
.popup-content {
  background: var(--fff);
  position: relative;
  transition: all .25s;

  .scroll-content,
  .content {
    padding: 30rpx;
    overflow: hidden;
  }

  .content {
    overflow-y: auto;
  }

  .popup-top {
    padding: 0 30rpx;
  }
}

.middle {
  border-radius: 20rpx;
  width: 90vw;
}

.right {
  width: 70vw;
  height: calc(100% - var(--window-top));
  position: fixed;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
}

.left {
  width: 70vw;
  height: calc(100% - var(--window-top));
  position: fixed;
  left: 0;
  bottom: 0;
  transform: translateX(-100%);
}

.bottom {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  border-radius: 15rpx 15rpx 0 0;
  transform: translateY(100%);
}

.top {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  border-radius: 0 0 15rpx 15rpx;
  transform: translateY(0%);

  .content {
    flex: 1;
    z-index: 999;
  }
}

.popup-content.middle,
.popup-content.bottom {
  .scroll-view,
  .content {
    max-height: 65vh;
  }
}

.popup-title {
  font-size: 26rpx;
  height: 90rpx;
  line-height: 90rpx;
  padding: 0 30rpx;
  text-align: left;
}

.popup-close {
  position: absolute;
  top: 15rpx;
  right: 15rpx;
  display: inline-block;
  width: 60rpx;
  height: 60rpx;
  line-height: 60rpx;
  text-align: center;
  font-size: 34rpx;
  color: var(--c999);
  z-index: 10;
}

.opt {
  //padding: 0 30rpx 30rpx;

  .btn-wrapper {
    display: flex;
    justify-content: space-between;
    //padding: 10rpx 30rpx 30rpx;

    button {
      flex: 1;
      height: 82rpx;
      line-height: 82rpx;
      font-size: 30rpx;
      border-radius: 10rpx;
      margin: 10rpx 30rpx 30rpx;
    }

    .submit {

    }

    .cancel {
      color: #666;
    }
  }
}

.popup-wrapper {
  position: fixed;
  width: 100vw;
  height: 100vh;
  top: 0;
  left: 0;
  z-index: 99;
  display: flex;
  justify-content: center;
  align-items: center;

  &.hide {
    display: none;
  }
}

.popup {
  position: relative;
  z-index: 1;
}

.modal {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transition: all .25s;
  opacity: 0;
  background: rgba(0, 0, 0, 0.5);
}

.show-modal {
  opacity: 1;
  transition: all .25s;
}

.hide-modal {
  opacity: 0;
  transition: all .25s;
}

.fade-leave-active,
.fade-enter-active {
  transition: all .25s;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}

.fade-enter-active .popup-content,
.fade-leave-active .popup-content {
  transition: all 0.25s;
}

.show-middle {
  //transform: scale3d(1, 1, 1);
  transition: all .25s;
}

.hide-middle {
  transform: scale3d(0, 0, 0);
  transition: all .25s;
}

.show-bottom {
  transform: translateY(0);
  transition: all .25s;
}

.hide-bottom {
  transform: translateY(100%);
  transition: all .25s;
}

.show-left {
  transform: translateX(0);
  transition: all .25s;
}

.hide-left {
  transform: translateX(-100%);
  transition: all .25s;
}

.show-right {
  transform: translateX(0);
  transition: all .25s;
}

.hide-right {
  transform: translateX(100%);
  transition: all .25s;
}

.show-top {
  transform: translateY(0);
  transition: all .25s;
}

.hide-top {
  transform: translateY(-100%);
  transition: all .25s;
}
</style>
